<template>
  <el-dialog title="批量新增" :visible="value" @close="close">
    <div class="box">
      <el-button type="text" @click="handleDownload">下载excel模板</el-button>
      <el-button type="text" @click="$refs.file.click()">上传excel</el-button>
      <!--  excel上传 选择本地文件 -->
      <input
        ref="file"
        type="file"
        accept=".xlsx,.xls"
        style="display: none"
        @change="handleChange"
      >
      <!-- <el-upload
   </div>
      class="upload-demo"
      action=""
      :on-change="handleChange"
      :auto-upload="false"
      :show-file-list="false"
    >
      <el-button size="mini" type="primary">选择文件</el-button>
    </el-upload> -->
    </div>
    <template #footer>
      <el-button size="mini" type="primary" @click="close">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getTemplateData, uploadExcel } from '@/api/employee'
import FileSaver from 'file-saver'

export default {
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    close() {
      this.$emit('input', false)
    },
    async handleDownload() {
      const res = await getTemplateData()
      FileSaver.saveAs(res, '员工信息导入模板.xlsx')
    },
    async handleChange(e) {
    //   console.log(e)
      if (e.target.files.length > 0) {
        console.log(e.target.files[0])

        try {
          await uploadExcel(e.target.files[0])
          // 提示
          this.$message.success('导入成功')
          // 关闭弹窗
          this.close()
          // 重新渲染
          this.$emit('refresh')
        //   this.$refs.file.value = ''
        } catch {
          // this.$refs.file.value = ''
        } finally {
          this.$refs.file.value = ''
        }
      }
    }
  }

}
</script>

<style lang="sass" scoped>
.box
  display: flex
  flex-direction: column
  width: 360px
  height: 180px
  border: 2px dashed #ccc
  justify-content: center
  border-radius: 10px
  margin: 0 auto
  align-items: center

</style>
